package com.wngbms.feigndemo.feign;

import com.wngbms.feigndemo.vo.BaseResp;
import feign.Logger;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(name = "caseFeign", configuration = CaseFeign.MultipartSupportConfig.class)
public interface CaseFeign {

    @PostMapping("/formData1")
    BaseResp formData1(@RequestParam String name, @RequestParam String password);

    @PostMapping(value = "/formData2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseResp formData2(@RequestPart("image") MultipartFile file);

    @PostMapping(value = "/formData3", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseResp formData3(@RequestParam String name, @RequestPart("image") MultipartFile file);

    // postman中对应的是form-data, 没有文件报错了
    @PostMapping(value = "/formData4", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseResp formData4(@RequestParam String name, @RequestParam String password);

    // postman中对应的是x-www-form-urlencoded 或者 form-data
    @PostMapping(value = "/formData5", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    BaseResp formData5(@RequestParam String name, @RequestParam String password);

    /**
     * body:defalutParam2=dpv2&defalutParam1=dpv1&123456789=
     *
     * @param body
     * @return
     */
    // 不加consumes, 会打印default-query-parameters, feign配置
    @PostMapping("/formData6")
    BaseResp formData6(@RequestBody String body);

    // JSON
    @PostMapping(value = "/formData7", consumes = MediaType.APPLICATION_JSON_VALUE)
    BaseResp formData7(@RequestBody String body);

    // XML
    @PostMapping(value = "/formData8", consumes = MediaType.APPLICATION_XML_VALUE)
    BaseResp formData8(@RequestBody String body);

    // HTML
    @PostMapping(value = "/formData9", consumes = MediaType.TEXT_HTML_VALUE)
    BaseResp formData9(@RequestBody String body);

    // TEXT
    @PostMapping(value = "/formData10", consumes = MediaType.TEXT_PLAIN_VALUE)
    BaseResp formData10(@RequestBody String body);

//    @PostMapping(value = "binary", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
//    BaseResp binary();

//    @Configuration
    class MultipartSupportConfig {
        @Bean
        public SpringFormEncoder feignFormEncoder() {
            return new SpringFormEncoder();
        }

        @Bean
        Logger.Level feignLoggerLeave(){
            return Logger.Level.FULL;
        }
    }
}
